package com.android.hw.projectmenu;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;

public class LCDBacklightLit extends Activity implements OnClickListener {
	
	private Button mBtnSetMaxBrightness;
	private Button mBtnSetDefaultBrightness;
	private Button mBtnOpenLongBacklight;
	private Button mBtnCloseLongBacklight;
	private int mDefaultBrightness = 255;
	private PowerManager.WakeLock mWl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.lcd_backlight_lit);
		mBtnSetMaxBrightness = (Button) findViewById(R.id.lcd_backlight_max);
		mBtnSetDefaultBrightness = (Button) findViewById(R.id.lcd_backlight_default);
		mBtnOpenLongBacklight = (Button) findViewById(R.id.lcd_long_backlight);
		mBtnCloseLongBacklight = (Button) findViewById(R.id.lcd_long_backlight_stop);
		mBtnSetMaxBrightness.setOnClickListener(this);
		mBtnSetDefaultBrightness.setOnClickListener(this);
		mBtnOpenLongBacklight.setOnClickListener(this);
		mBtnCloseLongBacklight.setOnClickListener(this);
		mDefaultBrightness = getScreenBrightness(this);
		PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
		mWl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "ChargingMenu"); 
	}
	
	public static int getScreenBrightness(Activity activity) {
		int value = 0;
		ContentResolver cr = activity.getContentResolver();
		try {
			value = Settings.System.getInt(cr,
					Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {

		}
		return value;
	}
	
	public static void setScreenBrightness(Activity activity, int value) {
	    WindowManager.LayoutParams params = activity.getWindow().getAttributes();
	    params.screenBrightness = value / 255f;
	    activity.getWindow().setAttributes(params);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.lcd_backlight_max:
			mBtnSetMaxBrightness.setEnabled(false);
			mBtnSetDefaultBrightness.setEnabled(true);
			setScreenBrightness(this, 255);
			break;
		case R.id.lcd_backlight_default:
			mBtnSetMaxBrightness.setEnabled(true);
			mBtnSetDefaultBrightness.setEnabled(false);
			setScreenBrightness(this, mDefaultBrightness);
			break;
		case R.id.lcd_long_backlight:
			mBtnOpenLongBacklight.setEnabled(false);
			mBtnCloseLongBacklight.setEnabled(true);
			mWl.acquire();
			break;
		case R.id.lcd_long_backlight_stop:
			mBtnOpenLongBacklight.setEnabled(true);
			mBtnCloseLongBacklight.setEnabled(false);
			mWl.release();
			break;

		default:
			break;
		}
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		if (mWl.isHeld()) {
			mWl.release();
		}
		setScreenBrightness(this, mDefaultBrightness);
	}
	
}
